package com.dats.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.StringJoiner;

/**
 * @author sig
 * @since 2016/9/25
 */
public class BytesUtil {

    public static byte[] file2Bytes(File file) throws IOException {
        int length = (int) file.length();
        byte[] bytes = new byte[length];
        InputStream is = new FileInputStream(file);
        int len = is.read(bytes);
        is.close();
        return bytes;
    }

    public static String encodeBase64(byte[] src) {
        return Base64.getEncoder().encodeToString(src);
    }

    public static byte[] decodeBase64(String src) {
        return Base64.getDecoder().decode(src);
    }

    public static String toNumString(byte[] bytes) {
        StringJoiner joiner = new StringJoiner(",");

        for (byte tmp : bytes)
            joiner.add(String .valueOf(tmp));
        return joiner.toString();
    }

    public static String toHexString(byte[] bytes) {
        StringJoiner joiner = new StringJoiner(",");

        for (byte tmp : bytes)
            joiner.add(Integer.toHexString(tmp));
        return joiner.toString();
    }
}
